/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vn.com.pps.web.common.component.table;

/**
 *
 * @author DaiMX
 */
public class ComparatorFactory
{
  public static AbstractColumnComparator getComparator(String pm_formattedAttribute, boolean pm_isAscending)
  {
    if (pm_formattedAttribute == null) return null;

    AttributeType type = getType(pm_formattedAttribute);
    if (type == AttributeType.STRING) return new StringColumnComparator(getMethodName(pm_formattedAttribute), pm_isAscending);
    if (type == AttributeType.NUMERIC) return new NumberColumnComparator(getMethodName(pm_formattedAttribute), pm_isAscending);

    throw new IllegalArgumentException("Type not yet supported (" + pm_formattedAttribute + ")");
  }

  private static String getMethodName(String pm_formattedAttribute)
  {
    int index = pm_formattedAttribute.indexOf("@");
    if (index > 0) pm_formattedAttribute = pm_formattedAttribute.substring(0, index);

    return pm_formattedAttribute.substring(0, 1).toUpperCase() + pm_formattedAttribute.substring(1);
  }

  private static AttributeType getType(String pm_formattedAttribute)
  {
    int index = pm_formattedAttribute.indexOf("@");
    if (index > 0) return AttributeType.get(pm_formattedAttribute.substring(index + 1));

    return AttributeType.STRING;
  }

  static class AttributeType
  {
    public static final AttributeType STRING = new AttributeType("String");

    public static final AttributeType NUMERIC = new AttributeType("Numeric");

    public static final AttributeType DATETIME = new AttributeType("Datetime");
    private String m_name;

    private AttributeType(String pm_name)
    {
      this.m_name = pm_name;
    }

    public String getName()
    {
      return this.m_name;
    }

    public static AttributeType get(String pm_name)
    {
      if (pm_name.equalsIgnoreCase(DATETIME.getName())) return DATETIME;
      if (pm_name.equalsIgnoreCase(NUMERIC.getName())) return NUMERIC;
      return STRING;
    }
  }
}
